package main;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author Administrator
 */
public class CustomTextBox {

    private final char[] KEY_NUM1_CHARS = {'.', '1', '!', '?'};
    private final char[] KEY_NUM2_CHARS = {'a', 'b', 'c', '2'};
    private final char[] KEY_NUM3_CHARS = {'d', 'e', 'f', '3'};
    private final char[] KEY_NUM4_CHARS = {'g', 'h', 'i', '4'};
    private final char[] KEY_NUM5_CHARS = {'j', 'k', 'l', '5'};
    private final char[] KEY_NUM6_CHARS = {'m', 'n', 'o', '6'};
    private final char[] KEY_NUM7_CHARS = {'p', 'q', 'r', 's', '7'};
    private final char[] KEY_NUM8_CHARS = {'t', 'u', 'v', '8'};
    private final char[] KEY_NUM9_CHARS = {'w', 'x', 'y', 'z', '9'};
    private final char[] KEY_NUM0_CHARS = {' ', '0'};
    private final char[] KEY_NUM1_CHARS_UP = {'.', '?', '!', '1'};
    private final char[] KEY_NUM2_CHARS_UP = {'A', 'B', 'C', '2'};
    private final char[] KEY_NUM3_CHARS_UP = {'D', 'E', 'F', '3'};
    private final char[] KEY_NUM4_CHARS_UP = {'G', 'H', 'I', '4'};
    private final char[] KEY_NUM5_CHARS_UP = {'J', 'K', 'L', '5'};
    private final char[] KEY_NUM6_CHARS_UP = {'M', 'N', 'O', '6'};
    private final char[] KEY_NUM7_CHARS_UP = {'P', 'Q', 'R', 'S', '7'};
    private final char[] KEY_NUM8_CHARS_UP = {'T', 'U', 'V', '8'};
    private final char[] KEY_NUM9_CHARS_UP = {'W', 'X', 'Y', 'Z', '9'};
    private final char[] KEY_NUM0_CHARS_UP = {' ', '0'};
    private final char[] KEY_NUM1 = {'1'};
    private final char[] KEY_NUM2 = {'2'};
    private final char[] KEY_NUM3 = {'3'};
    private final char[] KEY_NUM4 = {'4'};
    private final char[] KEY_NUM5 = {'5'};
    private final char[] KEY_NUM6 = {'6'};
    private final char[] KEY_NUM7 = {'7'};
    private final char[] KEY_NUM8 = {'8'};
    private final char[] KEY_NUM9 = {'9'};
    private final char[] KEY_NUM0 = {'0'};
    private char[] keynameQweryLowsCase = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v',
        'b', 'n', 'm', ',', '.', ' ', '@', '?', '"', '(', ')', '1', '2', '3', '*', '+', '=', '4', '5', '6',
        '#', '-', '7', '8', '9', '0', ';', ':', '!', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V',
        'B', 'N', 'M'};
    private int[] keycodeQwery = {113, 119, 101, 114, 116, 121, 117, 105, 111, 112, 97, 115, 100, 102, 103, 104, 106,
        107, 108, 122, 120, 99, 118, 98, 110, 109, 44, 46, 32, 64, 63, 34, 40, 41, 49, 50, 51, 42, 43, 61, 52, 53, 54,
        35, 45, 55, 56, 57, 48, 59, 58, 33, 81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 65, 83, 68, 70, 71, 72, 74, 75, 76, 90, 88, 67, 86, 66, 78, 77};
    private int[] keyCodeQwertyNumber = {109, 114, 116, 121, 102, 103, 104, 118, 98, 110};
    private String[] numberList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private boolean isFocus;
    private boolean isUseKeyPad;
    private boolean isShowAsterik;
    private boolean isShowTypeInput;
    private int textColor;
    private int borderColor;
    private int typeInput;
    private int textBoxX;
    private int textBoxY;
    private int textBoxWidth;
    private int textBoxHeight;
    int clearKeyCode = Integer.MIN_VALUE;
    StringBuffer currentText = new StringBuffer();
    StringBuffer passText;
    //private String currentString = "";
    int lastPressedKey = Integer.MIN_VALUE;
    int currentKeyStep = 0;
    int inputWidth = 0;
    int inputHeight = 17;
    int inputTranslationX = 0;
    int caretIndex = 0;
    int caretLeft = 0;
    long caretBlinkDelay = 500L;
    long lastCaretBlink = 0;
    long lastKeyTimestamp = 0;
    long maxKeyDelay = 500L;
    boolean goToNextChar = true;
    boolean caretBlinkOn = true;
    private String[] txtIntro = {"abc", "ABC", "123"};
    private char[] chars;
    private int boxCharTypeWidth;
    private boolean isOverWidth;
    private int startPos;

    public CustomTextBox(int borderColor, int textColor, boolean isShowAsterik, boolean keyPad) {
        isUseKeyPad = keyPad;
        isFocus = true;
        this.borderColor = borderColor;
        this.textColor = textColor;
        this.isShowAsterik = isShowAsterik;
        if (isUseKeyPad) {
            isShowTypeInput = true;
        }
        if (isShowAsterik) {
            passText = new StringBuffer();
        }
        boxCharTypeWidth = 42;
    }

   

    public void setTypeInput(int type) {
        typeInput = type;
    }

    public void setShowTypeInput(boolean isShow) {
        isShowTypeInput = isShow;
    }

    public void setFocus(boolean focus) {
        this.isFocus = focus;
        caretIndex = currentText.length();
        updateCaretPosition();
    }

    public void setText(String text) {
        if (!isShowAsterik) {
            currentText.setLength(0);
            currentText.append(text);
            caretIndex = currentText.length();
            updateCaretPosition();
        } else {
            currentText.setLength(0);
            passText.setLength(0);
            for (int i = 0; i < text.length(); i++) {
                currentText.append("*");
                passText.append(text.charAt(i));
            }
        }
    }

    public void setPass(String text) {
        passText.setLength(0);
        passText.append(text);
    }

    public void sizeChage(int w, int h) {
        if (w < h) {
            isUseKeyPad = true;
            //     paintKeyCode = "Numeric ";
        } else {
            isUseKeyPad = false;
            //    paintKeyCode = "Qwerty ";
        }
    }

    public void doSoftKeyRight() {
        if (isFocus) {
            deleteLeftChar();
            if (!isUseKeyPad) {
                updateCaretPosition();
            }
        }
    }

    public void doKeyPound() {
        changeInputType();
    }

    public void doInputChar(int keyCode) {
        if (isUseKeyPad) {
            checkNumbericKeyPad(keyCode);
        } else {
            checkQwertyKeyPad(keyCode);
        }
    }

    public void inputChar(int key) {
        if (isFocus) {
            doInputChar(key);
            if (isOverWidth) {
                startPos++;
            }
        }
    }

    public void poiterPressed(int x, int y) {
        if (textBoxX - 10 < x && x < textBoxX + textBoxWidth + 10
                && textBoxY - 10 < y && y < textBoxY + textBoxHeight + 10) {
            isFocus = true;
        }
    }

    public boolean isFocus() {
        return isFocus;
    }

    public String getText() {
        return currentText.toString();
    }

    public String getPassWord() {
        return passText.toString();

    }

    private void changeInputType() {
        typeInput = (++typeInput) % 3;
    }

    private char[] getChars(int key) {
        switch (key) {
            case Canvas.KEY_NUM1:
                if (typeInput == 0) {
                    return KEY_NUM1_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM1_CHARS_UP;
                } else {
                    return KEY_NUM1;
                }
            case Canvas.KEY_NUM2:
                if (typeInput == 0) {
                    return KEY_NUM2_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM2_CHARS_UP;
                } else {
                    return KEY_NUM2;
                }
            case Canvas.KEY_NUM3:
                if (typeInput == 0) {
                    return KEY_NUM3_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM3_CHARS_UP;
                } else {
                    return KEY_NUM3;
                }
            case Canvas.KEY_NUM4:
                if (typeInput == 0) {
                    return KEY_NUM4_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM4_CHARS_UP;
                } else {
                    return KEY_NUM4;
                }
            case Canvas.KEY_NUM5:
                if (typeInput == 0) {
                    return KEY_NUM5_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM5_CHARS_UP;
                } else {
                    return KEY_NUM5;
                }
            case Canvas.KEY_NUM6:
                if (typeInput == 0) {
                    return KEY_NUM6_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM6_CHARS_UP;
                } else {
                    return KEY_NUM6;
                }
            case Canvas.KEY_NUM7:
                if (typeInput == 0) {
                    return KEY_NUM7_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM7_CHARS_UP;
                } else {
                    return KEY_NUM7;
                }
            case Canvas.KEY_NUM8:
                if (typeInput == 0) {
                    return KEY_NUM8_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM8_CHARS_UP;
                } else {
                    return KEY_NUM8;
                }
            case Canvas.KEY_NUM9:
                if (typeInput == 0) {
                    return KEY_NUM9_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM9_CHARS_UP;
                } else {
                    return KEY_NUM9;
                }
            case Canvas.KEY_NUM0:
                if (typeInput == 0) {
                    return KEY_NUM0_CHARS;
                } else if (typeInput == 1) {
                    return KEY_NUM0_CHARS_UP;
                } else {
                    return KEY_NUM0;
                }
        }
        return null;
    }

    void updateCaretPosition() {
        caretLeft = PiPoDesigner.stringWidth(currentText.toString(), 0, caretIndex);
        int limitLength = (isShowTypeInput ? textBoxWidth - boxCharTypeWidth - 20 : textBoxWidth);
        if (textBoxWidth == 0) {
            return;
        }
        if (caretLeft > limitLength) {
            Debug.d("tran");
            isOverWidth = true;
//            if (startPos < currentText.length() - 1) {
//                startPos++;
//            }
            startPos = (caretLeft - limitLength) / PiPoDesigner.stringWidth("a");
            caretLeft = limitLength;
        } else {
            if (isOverWidth) {
                startPos = (caretLeft - limitLength) / PiPoDesigner.stringWidth("a");
                if (startPos == 0) {
                    isOverWidth = false;
                }
            }
        }
    }

    private void checkNumbericKeyPad(int key) {
        writeKeyPressed(key);
    }

    private void checkQwertyKeyPad(int key) {
        if (typeInput != 2) {
            for (int i = 0; i < keycodeQwery.length; i++) {
                if (key == keycodeQwery[i]) {
                    goToNextChar = true;
                    if (isShowAsterik) {
                        currentText.insert(caretIndex, '*');
                        passText.insert(caretIndex, keynameQweryLowsCase[i]);
                    } else {
                        currentText.insert(caretIndex, keynameQweryLowsCase[i]);
                    }
                    caretIndex++;
                    updateCaretPosition();
                    break;
                }
            }
            return;
        }
        for (int i = 0; i < keyCodeQwertyNumber.length; i++) {
            if (key == keyCodeQwertyNumber[i]) {
                goToNextChar = true;
                if (isShowAsterik) {
                    currentText.insert(caretIndex, '*');
                    passText.insert(caretIndex, numberList[i]);
                } else {
                    currentText.insert(caretIndex, numberList[i]);
                }
                caretIndex++;
                updateCaretPosition();
                break;
            }
        }
    }

    public void moveCurseurLeft() {
        if (!isFocus) {
            return;
        }
        if (caretIndex > 0) {
            caretIndex--;
            updateCaretPosition();
            goToNextChar = true;
        }
    }

    public void moveCurseurRight() {
        if (!isFocus) {
            return;
        }
        if (caretIndex < currentText.length()) {
            if (goToNextChar) {
                caretIndex++;
            }
            updateCaretPosition();
            goToNextChar = true;
        }
    }

    private void deleteLeftChar() {
        if (currentText.length() > 0 && caretIndex > 0) {
            caretIndex--;
            currentText.deleteCharAt(caretIndex);
            updateCaretPosition();
            if (isShowAsterik) {
                passText.deleteCharAt(caretIndex);
                updateCaretPosition();
            }
        }
    }

    private void paintBorder(Graphics g, int x, int y, int width, int height) {

        g.setColor(255, 255, 255);
        g.fillRoundRect(x - 1, y - 1, width - 8, height + 2, 7, 7);
        g.setColor(50, 25, 25);
        g.fillRoundRect(x, y, width - 10, height, 7, 7);
    }

    private void paintCharType(Graphics g, int x, int y, int width, int height) {
        if (isFocus) {
//            g.setColor(57, 34, 34);
//            g.fillRoundRect(x, y, width - 10, height, 7, 7);
            g.setColor(255, 0, 0);
            g.fillRect(x + width - boxCharTypeWidth, y + 1, 31, height - 2);
            PiPoDesigner.drawString(g, txtIntro[typeInput], PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, x + width - 40, y + 1);
        }
    }

    public void paint(Graphics g, int x, int y, int width, int height) {
        textBoxX = x;
        textBoxY = y;
        textBoxWidth = width;
        textBoxHeight = height;
        paintBorder(g, x, y, width, height);
        if (isShowTypeInput) {
            paintCharType(g, x, y, width, height);
        }
        x += 5;
        if (isOverWidth) {
            PiPoDesigner.drawString(g, currentText.toString(), startPos, currentText.length() - startPos, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, x, y);
        } else {
            PiPoDesigner.drawString(g, currentText.toString(), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, x, y);
        }
        if (caretBlinkOn && goToNextChar && isFocus) {
            g.setColor(0xffffff);
            g.drawLine(x + caretLeft, y, x + caretLeft, y + inputHeight);
        }
    }

    public void run() {
        if (isFocus) {
            checkTimestamps();
        }
    }

    private void showAsterik() {
        if (caretIndex == 0) {
            return;
        }
        currentText.setCharAt(caretIndex - 1, '*');
        updateCaretPosition();
    }

    public void checkTimestamps() {
        long currentTime = System.currentTimeMillis();

        if (lastCaretBlink + caretBlinkDelay < currentTime) {
            caretBlinkOn = !caretBlinkOn;
            lastCaretBlink = currentTime;
        }

        if (!goToNextChar && lastKeyTimestamp + maxKeyDelay < currentTime) {
            try {
                if (isShowAsterik) {
                    showAsterik();
                }
                goToNextChar = true;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void writeKeyPressed(int key) {
        if (goToNextChar || key != lastPressedKey) {
            goToNextChar = true;
            lastPressedKey = key;
            currentKeyStep = 0;
        } else {
            currentKeyStep++;
        }

        chars = getChars(key);

        if (chars != null) {
            if (currentKeyStep >= chars.length) {
                currentKeyStep -= chars.length;
            }
            if (goToNextChar) {
                currentText.insert(caretIndex, chars[currentKeyStep]);
                if (isShowAsterik) {
                    showAsterik();
                    passText.insert(caretIndex, chars[currentKeyStep]);

                }
                caretIndex++;

            } else {
                if (caretIndex <= 0) {
                    return;
                }
                currentText.setCharAt(caretIndex - 1, chars[currentKeyStep]);
                if (isShowAsterik) {
                    passText.setCharAt(caretIndex - 1, chars[currentKeyStep]);
                }
            }
//            currentString = currentText.toString();

            updateCaretPosition();

            lastKeyTimestamp = System.currentTimeMillis();

            goToNextChar = false;
        }
    }
}
